<?php
class Database{	
	var $host;
	var $password;
	var $base;
	var $db;
	var $isConnected;
	var $singleCall;
	
	function Database($host,$login,$pass,$base,$singleCall=false)
	{
		$this->host = $host;
		$this->login = $login;
		$this->password = $pass;
		$this->base= $base;
		$this->singleCall= $singleCall;
		$this->isConnected=false;
	}

	function connect()
	{
		$this->db = @mysql_connect($this->host,$this->login,$this->password);
		if(!$this->db) return 2; // Code erreur de connexion
		if(!@mysql_select_db($this->base)) return 3; // Code erreur de selection de base
		$this->isConnected = true;
		return 1; // Code de retour normal.
	}

	function query($query)
	{		
		if(!$this->isConnected){
			$this->connect();
		}
		$resultat = mysql_query($query) or die("Erreur d'execution de la requete - ".mysql_error()." Requete : ".$query);
		if($this->singleCall){
			$this->close();
		}
		return $resultat;
	}

	function close()
	{
		mysql_close();
		$this->isConnected=false;
	}
	
	function selectAll($table)
	{
		return $this->query("SELECT * FROM ".$table.";");
	}
	
	function install($mail,$pass){
		$this->query("CREATE TABLE IF NOT EXISTS accounts (
		  mail varchar(100) character set utf8 NOT NULL,
		  password varchar(100) character set utf8 NOT NULL,
		  isConfirmed tinyint(1) NOT NULL default '0',
		  isAdmin tinyint(1) NOT NULL default '0',
		  PRIMARY KEY  (mail)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
		
		$this->query("INSERT INTO accounts VALUES
		('".$mail."', '".md5($pass)."', 0, 1)");
		
		$this->query("CREATE TABLE IF NOT EXISTS colors (
		  colorName varchar(100) character set utf8 NOT NULL,
		  redQuantity int(3) NOT NULL default '0',
		  greenQuantity int(3) NOT NULL default '0',
		  blueQuantity int(3) NOT NULL default '0',
		  isActivated tinyint(1) NOT NULL,
		  PRIMARY KEY  (colorName)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
		
		$this->query("CREATE TABLE IF NOT EXISTS gameAnswers (
		  answerId int(11) NOT NULL auto_increment,
		  gameSessionId int(11) NOT NULL,
		  leftColorName varchar(100) NOT NULL,
		  rightColorName varchar(100) NOT NULL,
		  shapeName varchar(100) NOT NULL,
		  chosenColorName varchar(100) NOT NULL,
		  time varchar(100) NOT NULL,
		  PRIMARY KEY  (answerId)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
		
		$this->query("CREATE TABLE IF NOT EXISTS gameSessions (
		  gameSessionId int(11) NOT NULL auto_increment,
		  memberId varchar(100) character set utf8 NOT NULL,
		  moodId varchar(100) character set utf8 NOT NULL,
		  date varchar(100) NOT NULL,
		  isFinished tinyint(1) NOT NULL,
		  PRIMARY KEY  (gameSessionId)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
		
		
		$this->query("CREATE TABLE IF NOT EXISTS members (
		  memberId int(11) NOT NULL auto_increment,
		  accountMail varchar(100) NOT NULL,
		  nickname varchar(100) character set utf8 NOT NULL,
		  birthDate varchar(10) character set utf8 NOT NULL,
		  gender tinyint(1) NOT NULL,
		  savingResults tinyint(1) NOT NULL default '0',
		  PRIMARY KEY  (memberId)
		) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;");
		
		$this->query("CREATE TABLE IF NOT EXISTS moods (
		  moodName varchar(100) NOT NULL,
		  picture varchar(100) NOT NULL,
		  music varchar(100) NOT NULL,
		  isActivated tinyint(1) NOT NULL,
		  position int(11) NOT NULL,
		  PRIMARY KEY  (moodName)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
		
		$this->query("CREATE TABLE IF NOT EXISTS shapes (
		  shapeName varchar(100) NOT NULL,
		  javascriptFunction varchar(100) NOT NULL,
		  isActivated tinyint(1) NOT NULL,
		  PRIMARY KEY  (shapeName)
		) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
		
		$this->query("INSERT INTO shapes (shapeName, javascriptFunction, isActivated) VALUES
		('Cercle', 'createCircle', 1),
		('CarrÃ©', 'createSquare', 1),
		('Triangle', 'createTriangle', 1);");
		
		unlink($_SERVER['DOCUMENT_ROOT']."/modules/install.php");
	}
}
?>